package teaching.api;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import teaching.pojo.Student;
import teaching.pojo.User;
import teaching.service.StudentService;
import teaching.service.UserService;
import teaching.util.AuthAccess;
import teaching.util.Response;
import teaching.util.ResponseResult;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2024/3/2
 * Description: url: http://localhost:8080/user
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户登录接口")
public class UserAPI {

    @Resource
    private UserService userService;
    @Resource
    private StudentService studentService;

    @GetMapping("/getAllstudent")
    public ResponseResult<List<Student>> getAllstudent() {
        List<Student> studentList = studentService.getAllstudent();
        System.out.println(studentList);
        return Response.createOkResp("查询成功", studentList);
    }

    @PostMapping("/login")
    public ResponseResult<Object> login(@Valid User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Response.createFailResp("输入不合法");
        }
        String token = userService.login(user);
        return Response.createOkResp("登录成功", token);
    }

    @PostMapping("/studentLogin")
    public ResponseResult<Object> studentLogin(Student loginStudent) {
        if (StrUtil.isBlank(loginStudent.getUsername()) || StrUtil.isBlank(loginStudent.getPassword())) {
            return Response.createFailResp("输入不合法");
        }
        String token = studentService.login(loginStudent);
        return Response.createOkResp("登录成功", token);
    }

    @GetMapping("/userInfo")
    public ResponseResult<User> userInfo() {
//获取数据库的用户保存在userList中
        //从拦截器交到线程池里拿出用户数据
        User user = userService.getUserInfo();
        System.out.println(user);
        return Response.createOkResp("查询成功", user);
    }

    @PutMapping("/updateInfo")//教师修改个人信息
    public ResponseResult<Object> updateInfo(@RequestBody User newUser) {
        System.out.println("更新: " + newUser);
        boolean res = userService.updateById(newUser);
        if (!res) {
            return Response.createFailResp("修改失败");
        }
        return Response.createOkResp("修改成功", res);
    }

    @PostMapping("/register")
    public ResponseResult<Object> register(Student student) {
        System.out.println(student.toString());
        int num = studentService.register(student);
        return Response.createMsgResp("注册成功");
    }


    @GetMapping("/studentInfo")
    public ResponseResult<Student> studentInfo() {
        //从拦截器交到线程池里拿出用户数据
        Student student = studentService.getStudentInfo();
        return Response.createOkResp("查询成功", student);
    }

    @PutMapping("/updateStudentInfo")//教师修改个人信息
    public ResponseResult<Object> updateStudentInfo(@RequestBody Student newUser) {
        System.out.println("更新学生信息: " + newUser);
        boolean res = studentService.updateById(newUser);
        if (!res) {
            return Response.createFailResp("修改失败");
        }
        return Response.createOkResp("修改成功", res);
    }

    @GetMapping("/userName/{userId}")
    public ResponseResult<String> getUserName(@PathVariable("userId") Long userId) {
        System.out.println("userid==" + userId);
        //从拦截器交到线程池里拿出用户数据
        String name = userService.getUserName(userId);
        return Response.createOkResp("查询成功", name);
    }

    @GetMapping("/studentName/{studentId}")
    public ResponseResult<String> getStudentName(@PathVariable("studentId") Long studentId) {
        System.out.println("studentId====" + studentId);
        //从拦截器交到线程池里拿出用户数据
        String name = studentService.getStudentName(studentId);
        return Response.createOkResp("查询成功", name);
    }
}
